package com.skyline.wo.service;

import org.osm.jsoup.Jsoup;
import org.osm.jsoup.nodes.Document;
import org.osm.jsoup.nodes.Element;
import org.osm.jsoup.select.Elements;

import com.skyline.wo.model.Video;
import com.skyline.wo.type.VideoConstants;

/**
 * 视频网址视频信息提取工具类 支持以下网址：优酷、土豆、酷6、6间房、新浪、搜狐、56(我乐)、凤凰视频 版本更新说明： 1、2011-09-12 创建
 * 2、2011-09-13 新加对凤凰视频的支持，链接地址不在支持的列表中时返回原链接地址以及链接的页面标题
 * 
 * @author Guihua Liu
 * @version v1.0
 * @QQ 459041012
 * @email 850823422@139.com
 * @Website http://liuguihua.com
 */
public class VideoUtil {

	/**
	 * 获取视频信息
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static Video getVideoInfo(String url) throws Exception {
		Video video = null;

		if (url.indexOf(VideoConstants.VIDEO_DOMAIN_YOUKU) != -1) {
			try {
				video = getYouKuVideo(url);
			} catch (Exception e) {
				e.printStackTrace();
				video = null;
			}
		} 
		else if(url.indexOf(VideoConstants.VIDEO_DOMAIN_TUDOU_PLAYLIST)!=-1){
			try {
				video = getTudouPlayListVideo(url);
			} catch (Exception e) {
				video = null;
			}
		}else if (url.indexOf(VideoConstants.VIDEO_DOMAIN_TUDOU) != -1) {
			try {
				video = getTudouVideo(url);
			} catch (Exception e) {
				video = null;
			}
		} else if (url.indexOf(VideoConstants.VIDEO_DOMAIN_KU6) != -1) {
			try {
				video = getKu6Video(url);
			} catch (Exception e) {
				video = null;
			}
		} else if (url.indexOf(VideoConstants.VIDEO_DOMAIN_CN6) != -1) {
			try {
				video = get6Video(url);
			} catch (Exception e) {
				video = null;
			}
		} else if (url.indexOf(VideoConstants.VIDEO_DOMAIN_WOLE) != -1) {
			try {
				video = get56Video(url);
			} catch (Exception e) {
				video = null;
			}
		} else if (url.indexOf(VideoConstants.VIDEO_DOMAIN_SINA) != -1) {
			try {
				video = getSinaVideo(url);
			} catch (Exception e) {
				video = null;
			}
		} else if (url.indexOf(VideoConstants.VIDEO_DOMAIN_SOHU) != -1) {
			try {
				video = getSohuVideo(url);
			} catch (Exception e) {
				video = null;
			}
		} else if (url.indexOf(VideoConstants.VIDEO_DOMAIN_IFENG) != -1) {
			try {
				video = getIfengVideo(url);
			} catch (Exception e) {
				video = null;
			}
		} else if (url.indexOf(VideoConstants.VIDEO_DOMAIN_YINYUETAI) != -1) {
			try {
				video = getYinYueTaiVideo(url);
			} catch (Exception e) {
				video = null;
			}
		} else {
			// 链接地址不在支持的列表中时返回原链接地址以及链接的页面标题
			Document doc = getURLContent(url);
			video = new Video();
			video.setTitle(doc.title());
			video.setPageUrl(url);
		}

		return video;
	}

	/**
	 * 获取优酷视频
	 * 
	 * @param url
	 *            视频URL
	 */
	public static Video getYouKuVideo(String url) throws Exception {
		Document doc = getURLContent(url);

		/**
		 * 视频标题
		 */
		String title = doc.title().split("-")[0].trim();

		/**
		 * 获取视频缩略图
		 */
		String pic = getElementAttrById(doc, "s_sina", "href");
		int local = pic.indexOf("pic=");
		pic = pic.substring(local + 4);

		/**
		 * 获取视频地址
		 */
		String flash = getElementAttrById(doc, "link2", "value");

		/**
		 * 获取视频网页代码
		 */
		String htmlCode = getElementAttrById(doc, "link3", "value");

		/**
		 * 获取视频时间
		 */
		String time = getElementAttrById(doc, "download", "_href");
		String[] arrays = time.split("\\|");
		time = arrays[4];

		/**
		 * 获取视频简介
		 */
		String summary = "";
		try {
			Element el = doc.getElementById("long");
			summary = el.select(".item").get(0).html();
		} catch (Exception e) {
		}

		Video video = new Video();
		video.setTitle(title);
		video.setThumbnail(pic);
		video.setFlashUrl(flash);
		video.setTime(time);
		video.setSource("优酷视频");
		video.setPageUrl(url);
		video.setSummary(summary);
		video.setHtmlCode(htmlCode);

		return video;
	}
	/**
	 * 获取土豆视频
	 * 土豆的播放列表
	 * @author  burningcl
	 * 
	 * @param url
	 *            视频URL
	 */
	public static Video getTudouPlayListVideo(String url) throws Exception {
		Document doc = getURLContent(url);
		String content = doc.html();

		int beginLocal = content.indexOf("<script>var");
		int endLocal = content.indexOf("</script>");
		content = content.substring(beginLocal, endLocal);
		
		String[] playListInfo=url.split("playlist")[1].split("i");
		String id="";
		if(playListInfo.length==1){
			//URL中不带有播放的文件id
			 id = getScriptVarByName("defaultIid", content).trim();
			 content=content.split(id)[3];
			//System.out.println(id);
		}else{
			//URL带有播放的文件id
			 id = playListInfo[1].split("\\.")[0];
			 content=content.split(id)[1];
			//System.out.println(id);
		}
		
		
		/**
		 * 视频标题
		 */
		String title = "";
		try {
			//title:"
			title = content.split("title:\"")[1].split("\"")[0].trim();
		} catch (Exception e) {
		}

		/**
		 * 获取视频地址
		 */
		String flash = "";
			flash = content.split("icode:\"")[1].split("\"")[0];
			flash = "http://www.tudou.com/v/" + flash.trim() + "/v.swf";
		/**
		 * 获取视频缩略图
		 */
		String pic = "";
			pic = content.split("pic:\"")[1].split("\"")[0].trim();
			//pic = pic.trim();

		/**
		 * 视频简介
		 */
		String summary = "";
		try {
			summary = doc.select("meta[name=Description]").attr("content");
		} catch (Exception e) {
		}
		/**
		 * 获取视频时间
		 */
		String time = "";
		try {
			time = content.split("time:\"")[1].split("\"")[0].trim();
		} catch (Exception e) {
		}

		Video video = new Video();
		video.setTitle(title);
		video.setThumbnail(pic);
		video.setFlashUrl(flash);
		video.setTime(time);
		video.setSource("土豆视频");
		video.setPageUrl(url);
		video.setSummary(summary);
		video.setHtmlCode(getHtmlCode(flash));

		return video;
	}
	/**
	 * 获取土豆视频
	 * 
	 * @param url
	 *            视频URL
	 */
	public static Video getTudouVideo(String url) throws Exception {
		Document doc = getURLContent(url);
		String content = doc.html();

		int beginLocal = content.indexOf("<script>document.domain");
		int endLocal = content.indexOf("</script>");
		content = content.substring(beginLocal, endLocal);
		/**
		 * 视频标题
		 */
		String title = "";
		try {
			title = doc.title().split("_")[0].trim();
		} catch (Exception e) {
		}

		/**
		 * 获取视频地址
		 */
		String flash = "";
			flash = getScriptVarByName("iid_code", content);
			flash = "http://www.tudou.com/v/" + flash.split("=")[1].trim() + "/v.swf";
		/**
		 * 获取视频缩略图
		 */
		String pic = "";
			pic = getScriptVarByName("thumbnail", content);
			pic = pic.split("=")[1].trim();

		/**
		 * 视频简介
		 */
		String summary = "";
		try {
			summary = doc.select("meta[name=Description]").attr("content");

		} catch (Exception e) {
		}
		/**
		 * 获取视频时间
		 */
		String time = "";
		try {
			time = getScriptVarByName("time", content);
		} catch (Exception e) {
		}

		Video video = new Video();
		video.setTitle(title);
		video.setThumbnail(pic);
		video.setFlashUrl(flash);
		video.setTime(time);
		video.setSource("土豆视频");
		video.setPageUrl(url);
		video.setSummary(summary);
		video.setHtmlCode(getHtmlCode(flash));

		return video;
	}

	/**
	 * 获取酷6视频
	 * 
	 * @param url
	 *            视频URL
	 */
	public static Video getKu6Video(String url) throws Exception {
		Document doc = getURLContent(url);

		/**
		 * 获取视频标题
		 */
		String title = doc.title().split(" ")[0].trim();

		/**
		 * 获取视频地址
		 */
		String flash = getElementAttrById(doc, "outSideSwfCode", "value");

		/**
		 * 获取视频地址
		 */
		String htmlCode = getElementAttrById(doc, "outSideHtmlCode", "value");

		/**
		 * 获取视频缩略图
		 */
		Element picEt = doc.getElementById("plVideosList");
		String time = null;
		String pic = null;
		if (picEt != null) {
			Elements pics = picEt.getElementsByTag("img");
			pic = pics.get(0).attr("src");

			/**
			 * 获取视频时长
			 */
			Element timeEt = picEt.select("span.review>cite").first();
			time = timeEt.text();
		} else {
			pic = doc.getElementsByClass("s_pic").first().text();
		}

		/**
		 * 视频简介
		 */
		String summary = doc.select("meta[name=Description]").attr("content");

		Video video = new Video();
		video.setTitle(title);
		video.setThumbnail(pic);
		video.setFlashUrl(flash);
		video.setTime(time);
		video.setSource("酷6视频");
		video.setPageUrl(url);
		video.setSummary(summary);
		video.setHtmlCode(htmlCode);
		return video;

	}

	/**
	 * 获取6间房视频
	 * 
	 * @param url
	 *            视频URL
	 */
	public static Video get6Video(String url) throws Exception {
		Document doc = getURLContent(url);

		/**
		 * 视频标题
		 */
		String title = doc.title().split("-")[0].trim();

		/**
		 * 获取视频缩略图
		 */
		Element picEt = doc.getElementsByClass("summary").first();
		String pic = picEt.getElementsByTag("img").first().attr("src");

		/**
		 * 视频简介
		 */
		String summary = doc.select("meta[name=Description]").attr("content");

		/**
		 * 获取视频地址
		 */
		Element flashEt = doc.getElementById("video-share-code");
		doc = Jsoup.parse(flashEt.attr("value"));
		String flash = doc.select("embed").attr("src");

		Video video = new Video();
		video.setTitle(title);
		video.setThumbnail(pic);
		video.setFlashUrl(flash);
		video.setSource("6间房");
		video.setPageUrl(url);
		video.setSummary(summary);
		video.setHtmlCode(getHtmlCode(flash));

		return video;
	}

	/**
	 * 获取56视频
	 * 
	 * @param url
	 *            视频URL
	 */
	public static Video get56Video(String url) throws Exception {
		Document doc = getURLContent(url);
		String content = doc.html();

		/**
		 * 视频标题
		 */
		String title = doc.getElementById("VideoTitle").select("h1").text();

		/**
		 * 获取视频缩略图
		 */
		int begin = content.indexOf("\"img\":\"");
		content = content.substring(begin + 7, begin + 200);
		int end = content.indexOf("\"};");
		String pic = content.substring(0, end).trim();
		pic = pic.replaceAll("\\\\", "");

		/**
		 * 获取视频地址
		 */
		String flash = "http://player.56.com" + url.substring(url.lastIndexOf("/"), url.lastIndexOf(".html")) + ".swf";

		/**
		 * 视频简介
		 */
		String summary = doc.select("meta[name=Description]").attr("content");

		Video video = new Video();
		video.setTitle(title);
		video.setThumbnail(pic);
		video.setFlashUrl(flash);
		video.setSource("56视频");
		video.setPageUrl(url);
		video.setSummary(summary);
		video.setHtmlCode(getHtmlCode(flash));
		return video;
	}

	/**
	 * 获取新浪视频
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static Video getSinaVideo(String url) throws Exception {
		Document doc = getURLContent(url);

		/**
		 * 视频标题
		 */
		String title = doc.getElementById("videoTitle").text();

		/**
		 * 视频简介
		 */
		String summary = doc.getElementById("videoContent").text();

		String content = doc.html();
		int beginLocal = content.indexOf("document.domain");
		int endLocal = content.indexOf("</script>");
		content = content.substring(beginLocal + 2, endLocal);

		/**
		 * 视频缩略图
		 */
		String pic = getScriptVarByName("pic", content);

		/**
		 * flash地址
		 */
		String flash = getScriptVarByName("swfOutsideUrl", content);

		Video video = new Video();
		video.setTitle(title);
		video.setThumbnail(pic);
		video.setFlashUrl(flash);
		video.setSource("新浪视频");
		video.setPageUrl(url);
		video.setSummary(summary);
		video.setHtmlCode(getHtmlCode(flash));
		return video;
	}

	/**
	 * 获取搜狐视频
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static Video getSohuVideo(String url) throws Exception {
		Document doc = getURLContent(url);

		/**
		 * 视频标题
		 */
		String title = doc.title().split("-")[0].trim();

		/**
		 * 视频简介
		 */
		String summary = doc.select(".vIntro.clear > p").text();

		/**
		 * 视频缩略图
		 */
		String thumbnail = doc.getElementById("thumbnail").attr("src");

		String videoId = thumbnail.split("_")[2];

		/**
		 * 视频FLASH地址
		 */
		String flash = "http://share.vrs.sohu.com/" + videoId + "/v.swf&autoplay=false";

		Video video = new Video();
		video.setTitle(title);
		video.setThumbnail(thumbnail);
		video.setFlashUrl(flash);
		video.setSource("搜狐视频");
		video.setPageUrl(url);
		video.setSummary(summary);
		video.setHtmlCode(getHtmlCode(flash));
		return video;
	}

	/**
	 * 获取凤凰视频
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static Video getIfengVideo(String url) throws Exception {
		Document doc = getURLContent(url);
		String content = doc.html();
		int beginLocal = content.indexOf("videoinfo={");
		int endLocal = content.indexOf("</script>");
		content = content.substring(beginLocal, endLocal);
		content = content.replaceAll("\"", "").replaceAll("\n", "").trim();
		content = content.substring(11, content.length() - 2);
		String[] params = content.split(",");

		/**
		 * 视频标题
		 */
		String title = doc.select(".playtitle > h1").text();

		/**
		 * 视频简介
		 */
		String summary = doc.getElementById("full_des").text();
		summary = summary.replaceAll("<<收起", "");

		/**
		 * 视频FLASH地址
		 */
		String videoId = url.substring(url.lastIndexOf("/"), url.lastIndexOf(".shtml"));
		String flash = "http://v.ifeng.com/include/exterior.swf?guid=" + videoId + "&AutoPlay=false";

		/**
		 * 视频缩略图
		 */
		String thumbnail = params[5].substring(4, params[5].length());

		/**
		 * 视频时长
		 */
		String time = params[3].substring(9, params[3].length());
		long ss = Long.parseLong(time);
		long hour = ss / 3600;
		long minute = ss % 3600 / 60;
		long second = ss % 60;

		Video video = new Video();
		video.setTitle(title);
		video.setThumbnail(thumbnail);
		video.setFlashUrl(flash);
		video.setTime(hour + ":" + minute + ":" + second);
		video.setSource("凤凰视频");
		video.setPageUrl(url);
		video.setSummary(summary);
		video.setHtmlCode(getHtmlCode(flash));
		return video;
	}

	/**
	 * 获取音悦台MV
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static Video getYinYueTaiVideo(String url) throws Exception {
		Document doc = getURLContent(url);

		/**
		 * 获取视频地址
		 */
		String flash = getElementAttrById(doc, "flashCode", "value");

		/**
		 * 视频标题
		 */
		String title = doc.getElementById("videoTitle").html() + " - " + doc.getElementById("videoArtistName").html();

		/**
		 * 视频内容
		 */
		String summary = doc.getElementById("videoContent").select("span").first().html();

		/**
		 * 视频缩略图
		 */
		String thumbnail = doc.select("meta[property=og:image]").attr("content");

		Video video = new Video();
		video.setTitle(title);
		video.setThumbnail(thumbnail);
		video.setFlashUrl(flash);
		video.setSource("音悦台MV");
		video.setPageUrl(url);
		video.setSummary(summary);
		video.setHtmlCode(getHtmlCode(flash));
		return video;
	}

	/**
	 * 获取script某个变量的值
	 * 
	 * @param name
	 *            变量名称
	 * @return 返回获取的值
	 */
	private static String getScriptVarByName(String name, String content) {
		String script = content;
		int begin = script.indexOf(name);
		script = script.substring(begin + name.length() + 2);
		int end = script.indexOf(",");
		script = script.substring(0, end);
		String result = script.replaceAll("'", "");
		return result.trim();
	}

	/**
	 * 根据HTML的ID键及属于名，获取属于值
	 * 
	 * @param id
	 *            HTML的ID键
	 * @param attrName
	 *            属于名
	 * @return 返回属性值
	 */
	private static String getElementAttrById(Document doc, String id, String attrName) throws Exception {
		Element et = doc.getElementById(id);
		String attrValue = et.attr(attrName);
		return attrValue;
	}

	/**
	 * 根据FLASH地址生成页面代码
	 * 
	 * @param flashUrl
	 * @return
	 * @throws Exception
	 */
	private static String getHtmlCode(String flashUrl) throws Exception {
		return "<embed src=\""
				+ flashUrl
				+ "\" allowFullScreen=\"true\" quality=\"high\" width=\"480\" height=\"400\" align=\"middle\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\"></embed>";
	}

	/**
	 * 获取网页的内容
	 */
	private static Document getURLContent(String url) throws Exception {
		Document doc = Jsoup.connect(url).data("query", "Java").userAgent("Mozilla").cookie("auth", "token").timeout(5000).get();
		return doc;
	}

}